package com.java.test.interview;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author TonyOne
 * @version 1.0
 * @desc 来自http://www.importnew.com/22083.html
 * @date 2018/10/9 9:11
 * @company
 */
public class Interview150 {

    @Test
    public void testFloat() {
        float f=3.4f;
        System.out.println(f);
    }

    @Test
    public void testShort() {
        //short s1 = 1; s1 = s1 + 1;
        short s1 = 1; s1 += 1;
        System.out.println(s1);
    }

    @Test
    public void testInteger() {
        Integer a = new Integer(3);
        Integer b = 3;                  // 将3自动装箱成Integer类型
        int c = 3;
        System.out.println(a == b);     // false 两个引用没有引用同一对象
        System.out.println(a.equals(b));     // false 两个引用没有引用同一对象
        System.out.println(a == c);     // true a自动拆箱成int类型再和c比较
        System.out.println(b == c);
    }

    @Test
    public void testInteger2() {
        Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150;
        System.out.println(f1 == f2);
        System.out.println(f3 == f4);
    }

    @Test
    public void testSimulateGoto() {
        int temp = 0;
        out: // label
        for (int i = 0; i < 3; ++i) {
            System.out.println("I am here");
            for (int j = 0; j < 20; ++j) {
                if(temp==0) {
                    System.out.println("j: " + j);
                    if (j == 1) {
                        temp = j;
                        continue out; // goto label "out"
                        //break out; // goto label "out"
                    }
                }
            }
        }
        System.out.println("temp = " + temp);
    }

    @Test
    public void testSimulateGoto2() {
        boolean t = true;
        first: {
            second: {
                third: {
                    System.out.println("Before the break");

                    if (t) {
                        break second;
                    }

                    System.out.println("Not executed");
                }

                System.out.println("Not executed - end of second block");
            }

            System.out.println("End of third block");
        }
    }

    @Test
    public void testMath() {
        System.out.println(Math.round(11.5));
        System.out.println(Math.round(-11.5));
        System.out.println(Math.round(11.4));
        System.out.println(Math.round(-11.6));
        System.out.println(2 << 3);
//        选择31是因为可以用移位和减法运算来代替乘法，从而得到更好的性能。说到这里你可能已经想到了：31 * num 等价于(num << 5) – num，
//        左移5位相当于乘以2的5次方再减去自身就相当于乘以31，现在的VM都能自动完成这个优化。
        int num = 10;
        System.out.println((31 * 10)==((10 << 5)-10));
    }

    @Test
    public void testString() {
        String s1 = "Programming";
        String s2 = new String("Programming");
        String s3 = "Program" + "ming";
        System.out.println(s1 == s2);
        System.out.println(s1 == s3);
        System.out.println(s1 == s1.intern());
        char c = '中';
        System.out.println(c);
    }

    public String reverse(String originStr) {
        if(originStr == null || originStr.length() <= 1)
            return originStr;
        return reverse(originStr.substring(1)) + originStr.charAt(0);
    }

    @Test
    public void testReverseString() {
        System.out.println(reverse("123456"));
        Object a;
    }

    //面试题： - 如果要从字符串中截取第一个英文左括号之前的字符串，
    // 例如：北京市(朝阳区)(西城区)(海淀区)，截取结果为：北京市，那么正则表达式怎么写？
    // 下面的正则表达式中使用了懒惰匹配和前瞻，如果不清楚这些内容，推荐读一下网上很有名的《正则表达式30分钟入门教程》。

    /**
     * (?=exp)	匹配exp前面的位置
     * (?<=exp)	匹配exp后面的位置
     */
    @Test
    public void testRegex() {
        String str = "北京市(朝阳区)(西城区)(海淀区)";
        Pattern p = Pattern.compile(".*?(?=\\()");
        Matcher m = p.matcher(str);
        if(m.find()) {
            System.out.println(m.group());
        }
    }

    @Test
    public void testReflect() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        System.out.println(String.class);
        System.out.println("hello".getClass());
        System.out.println(Class.forName("java.lang.String"));

        System.out.println(String.class.newInstance());
        System.out.println(String.class.getConstructor(String.class).newInstance("Hello"));

        String str = "hello";
        Method m = str.getClass().getMethod("toUpperCase");
        System.out.println(m.invoke(str));  // HELLO

    }

    @Test
    public void HASH_COLLISION_DOS() {
        // Aa和BB
        System.out.println("Aa".hashCode());
        System.out.println("BB".hashCode());
        String[] arr = {};
        System.out.println(arr.toString());
    }
}
